<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<title>讨论区</title>
		<link rel="stylesheet" href="../css/mui.min.css">
		<script src="../js/mui.min.js"></script>
		<script src="../js/jquery.min.js"></script>
	</head>
	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">讨论区</h1>
			<span class="mui-icon mui-icon mui-icon-plusempty mui-spin mui-pull-right" onclick="createQuestion()"></span>
		</header>

		<div class="mui-content" id="content">
			<!-- -------------------数据填充---------------------- -->
			<!-- <div class="mui-card" onclick="mui.openWindow({url:'discussion_detail.html'})">
				<div class="mui-card-header">链表问题</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						怎么实现链表反转
					</div>
				</div>
				<div class="mui-card-footer">
					<span>张三</span>
					<span>2019-08-12</span>
				</div>
			</div>
			<div class="mui-card" onclick="mui.openWindow({url:'discussion_detail.html'})">
				<div class="mui-card-header">链表问题</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						怎么实现链表反转
					</div>
				</div>
				<div class="mui-card-footer">
					<span>张三</span>
					<span>2019-08-12</span>
				</div>
			</div> -->
			<!-- -------------------数据填充---------------------- -->
		</div>

		<script>
			mui.init({
				swipeBack: true, //启用右滑关闭功能
			});

			var cID;
			var uID;
			mui.plusReady(function() {
				cID = plus.webview.currentWebview().cID;
				uID = plus.storage.getItem('uID');
				init();
			})

			function init() {
				mui.ajax({
					url: 'http://120.27.247.180:8080/School/discussion/queryDiscussion',
					type: "post",
					data: {
						"cID": cID
					},
					dataType: "jsonp",
					contentType: "application/x-www-form-urlencoded; charset=utf-8",
					jsonp: "jsoncallback",
					success: function(data) {
						data = JSON.parse(data);
						//讨论区
						var Ele = jQuery("#content");
						Ele.empty();
						var Info = data.disList;
						var name = data.nameList;
						for (var i = 0, l = Info.length; i < l; i++) {
							var date = new Date(Info[i]['issueTime']);
							var time = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate();
							var tempEle = '<div class="mui-card" onclick="mui.openWindow({url:\'discussion_detail.html\', ' +
								'extras:{' +
								'\'dID\':' + Info[i]['dID'] +
								'}})"> ' +
								'<div class="mui-card-header">' + Info[i]['title'] + '</div>' +
								'<div class="mui-card-content">' +
								'<div class="mui-card-content-inner">' + Info[i]['description'] + '</div></div>' +
								'<div class="mui-card-footer">' +
								'<span>' + name[i] + '</span>' +
								'<span>' + time + '</span>' +
								'</div></div>';
							Ele.append(tempEle);
						}
					},
					error: function() {
						mui.toast("服务器异常，请稍后再试");
					}
				});
			}

			function createQuestion() {
				var title, des;
				var btnArray = ['取消', '确定'];
				mui.prompt('输入问题标题：', '', '提示', btnArray, function(e) {
					if (e.index == 1) {
						title = e.value;
						var btnArray2 = ['取消', '确定'];
						mui.prompt('输入详细描述：', '', '提示', btnArray2, function(e2) {
							if (e2.index == 1) {
								des = e2.value;
								mui.ajax({
									url: 'http://120.27.247.180:8080/School/discussion/discuss',
									type: "post",
									data: {
										"title": title,
										"description": des,
										"cID": cID,
										"uID": uID,
									},
									dataType: "jsonp",
									contentType: "application/x-www-form-urlencoded; charset=utf-8",
									jsonp: "jsoncallback",
									success: function(data) {
										data = JSON.parse(data);
										if (data['suc'])
											mui.alert('发布成功', '提示', function() {
												init();
											});
										else
											mui.toast("发布失败");
									},
									error: function() {
										mui.toast("服务器异常，请稍后再试");
									}
								});

							}
						});

					}
				});
			}
		</script>
	</body>
</html>
